﻿using IveelySE.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IveelySE.Index
{

    /// <summary>
    /// 二维表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public class DimensionTable<T>
    {
        /// <summary>
        /// 列
        /// </summary>
        /// <typeparam name="T">列索引类型(一般用String或Int)</typeparam>
        [Serializable]
        public class Cloumn<T>
        {
            /// <summary>
            /// 列存储器
            /// </summary>
            private readonly Hashtable _table = new Hashtable();

            /// <summary>
            /// 索引器
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            public object this[T index]
            {
                get { return _table[index]; }
                set
                {
                    _table[index] = value;
                }
            }

            /// <summary>
            /// 获取所有的列集合
            /// (将一列的所有提取出来)
            /// </summary>
            /// <returns></returns>
            public DSortedList<double> GetAllValue()
            {
                DSortedList<double> result = new DSortedList<double>();
                foreach (DictionaryEntry row in _table)
                {
                    result.Add(double.Parse(row.Value.ToString()));
                }
                return result;
            }


        }

        /// <summary>
        /// 列集合
        /// </summary>
        private readonly Hashtable _cloumns = new Hashtable();

        /// <summary>
        /// 列访问索引器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Cloumn<T> this[T index]
        {
            get
            {
                if (!_cloumns.ContainsKey(index))
                {
                    _cloumns.Add(index, new Cloumn<T>());
                }
                return (Cloumn<T>)_cloumns[index];
            }
            set { _cloumns[index] = value; }
        }

        /// <summary>
        /// 根据行名字，获取整行数据
        /// 类似于我们选中某行，我们将选中某行的数据
        /// 以列表的形式取出
        /// </summary>
        /// <returns></returns>
        public DSortedList<double> GetRowByName(T name)
        {
            if (!_cloumns.ContainsKey(name))
            {
                return null;
            }
            return ((Cloumn<T>)_cloumns[name]).GetAllValue();
        }

        /// <summary>
        /// 根据列的名字清楚列数据(不建议常用，效率较低，会去读取每一行然后去删除该列)
        /// </summary>
        /// <param name="name"></param>
        public void CleanByColumnName(T name)
        {
            //读取每一行
            foreach (DictionaryEntry cloumn in _cloumns)
            {
                //删除改行中所有相关数据
                ((Hashtable)cloumn.Value).Remove(name);
            }
        }
    }
}
